package com.hyacinth.lotterysystem.common.interceptor;

import com.hyacinth.lotterysystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        if (token == null){
            log.info("获取token为null, 请求接口: "+request.getRequestURI());
            response.setStatus(401);
            return false;
        }
        Claims claims = JWTUtil.parseJWT(token);
        if (claims == null){
            response.setStatus(401);
            log.info("解析token失败, 请求接口: "+request.getRequestURI());
            return false;
        }
        return true;
    }
}
